package com.huawei.dms.rocketmq.topic;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.rocketmq.v2.RocketMQClient;
import com.huaweicloud.sdk.rocketmq.v2.model.CreateTopicOrBatchDeleteTopicReq;
import com.huaweicloud.sdk.rocketmq.v2.model.CreateTopicOrBatchDeleteTopicRequest;
import com.huaweicloud.sdk.rocketmq.v2.model.CreateTopicOrBatchDeleteTopicResponse;
import com.huaweicloud.sdk.rocketmq.v2.model.DeleteTopicRequest;
import com.huaweicloud.sdk.rocketmq.v2.model.DeleteTopicResponse;
import com.huaweicloud.sdk.rocketmq.v2.model.ShowOneTopicRequest;
import com.huaweicloud.sdk.rocketmq.v2.model.ShowOneTopicResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class RocketMQTopicDemo {
    private static final Logger logger = LoggerFactory.getLogger(RocketMQTopicDemo.class);

    public static void main(String[] args) {
        String ak = "<YOUR ak>";
        String sk = "<YOUR sk>";
        String endpoint = "<YOUR Endpoint String>";
        String projectId = "<YOUR Project ID>";
        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk)
                .withProjectId(projectId);
        RocketMQClient client = RocketMQClient.newBuilder()
                .withCredential(auth)
                .withEndpoint(endpoint)
                .build();
        // 创建topic
        createTopic(client);
        // 查询topic
        listOneTopic(client);
        // 删除topic
        deleteTopic(client);
    }

    private static void createTopic(RocketMQClient client) {
        try {
            CreateTopicOrBatchDeleteTopicRequest request = new CreateTopicOrBatchDeleteTopicRequest();
            CreateTopicOrBatchDeleteTopicReq body = new CreateTopicOrBatchDeleteTopicReq();
            request.withInstanceId("<YOUR InstanceId>");
            // topic 名称
            body.withName("test-topic");
            // 代理
            List<String> brokers = new ArrayList<>();
            brokers.add("broker-0");
            body.withBrokers(brokers);
            // 队列数
            body.withQueueNum(new BigDecimal(3));
            // 权限
            // sub 发布
            // pub 订阅
            // all 发布+订阅
            body.withPermission(CreateTopicOrBatchDeleteTopicReq.PermissionEnum.fromValue("all"));
            request.withBody(body);
            CreateTopicOrBatchDeleteTopicResponse response = client.createTopicOrBatchDeleteTopic(request);
            logger.info(String.valueOf(response.toString()));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private static void deleteTopic(RocketMQClient client) {
        try {
            DeleteTopicRequest request = new DeleteTopicRequest();
            request.withInstanceId("<YOUR InstanceId>");
            request.withTopic("test-topic");
            DeleteTopicResponse response = client.deleteTopic(request);
            logger.info(String.valueOf(response.toString()));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private static void listOneTopic(RocketMQClient client) {
        try {
            ShowOneTopicRequest request = new ShowOneTopicRequest();
            request.withInstanceId("<YOUR InstanceId>");
            request.withTopic("test-topic");
            ShowOneTopicResponse response = client.showOneTopic(request);
            logger.info(String.valueOf(response.toString()));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
